﻿using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using Zvezda.FSO2.DB;

namespace FSO
{
    public partial class UserControlSearchOrganization : UserControl, InterfaceSearchControl
    {
        private bool _openOnDblClick = false;

        public UserControlSearchOrganization()
        {
            InitializeComponent();
        }

        public void InitializeGridView()
        {
            GridViewTextBoxColumn colShortName = new GridViewTextBoxColumn(dataSetFSO1.NSI_ORGANIZATION.SHORT_NAMEColumn.ColumnName);
            colShortName.HeaderText = "Наименование";

            GridViewTextBoxColumn colInn = new GridViewTextBoxColumn(dataSetFSO1.NSI_ORGANIZATION.INNColumn.ColumnName);
            colInn.HeaderText = "ИНН";

            GridViewTextBoxColumn colKpp = new GridViewTextBoxColumn(dataSetFSO1.NSI_ORGANIZATION.KPPColumn.ColumnName);
            colKpp.HeaderText = "КПП";

            GridViewTextBoxColumn colOkpo = new GridViewTextBoxColumn(dataSetFSO1.NSI_ORGANIZATION.OKPOColumn.ColumnName);
            colOkpo.HeaderText = "ОКПО";

            GridViewTextBoxColumn colCity = new GridViewTextBoxColumn(dataSetFSO1.NSI_ORGANIZATION.CITYColumn.ColumnName);
            colCity.HeaderText = "Город";

            radGridViewSearhOrganization.MasterTemplate.Columns.Clear();
            radGridViewSearhOrganization.MasterTemplate.Columns.Add(colShortName);
            radGridViewSearhOrganization.MasterTemplate.Columns.Add(colInn);
            radGridViewSearhOrganization.MasterTemplate.Columns.Add(colKpp);
            radGridViewSearhOrganization.MasterTemplate.Columns.Add(colOkpo);
            radGridViewSearhOrganization.MasterTemplate.Columns.Add(colCity);

            radGridViewSearhOrganization.DataSource = dataSetFSO1.NSI_ORGANIZATION;

        }

        #region InterfaceSearchControl members

        public bool OpenOnDoubleClick
        {
            get
            {
                return _openOnDblClick;
            }
            set
            {
                _openOnDblClick = value;
            }
        }

        public bool EnableFiltering
        {
            get
            {
                return radGridViewSearhOrganization.EnableFiltering;
            }
            set
            {
                radGridViewSearhOrganization.EnableFiltering = value;
            }
        }

        public DataRow SelectedRow
        {
            get 
            {
                DataRow dr;
                if (radGridViewSearhOrganization.SelectedRows.Count > 0)
                {
                    dr = (((DataRowView)(radGridViewSearhOrganization.SelectedRows.First()).DataBoundItem)).Row;
                }
                else
                {
                    dr = null;
                }
                return dr;
            }
        }

        public GridViewSelectedRowsCollection SelectedRows
        {
            get
            {
                return radGridViewSearhOrganization.SelectedRows;
            }
        }

        public DataSet OwnerDataSet
        {
            get { return dataSetFSO1; }
            set { dataSetFSO1 = (DataSetFSO)value; }
        }

        public void RefreshList()
        {
            nsI_ORGANIZATIONTableAdapter1.Fill(dataSetFSO1.NSI_ORGANIZATION);
        }

        #endregion

        #region Events

        private void UserControlSearchOrganization_Load(object sender, EventArgs e)
        {
            nsI_ORGANIZATIONTableAdapter1.Fill(dataSetFSO1.NSI_ORGANIZATION);

            InitializeGridView();
        }

        private void radGridViewSearhOrganization_CellDoubleClick(object sender, GridViewCellEventArgs e)
        {
            DataRow selectRow = this.SelectedRow;
            if (selectRow != null && _openOnDblClick)
            {               
                FormNSIOrganization formOrg = new FormNSIOrganization((DataSetFSO.NSI_ORGANIZATIONRow)selectRow);
                if (!FormHelper.HasOpenedForm(formOrg.FormName))
                {
                    formOrg.MdiParent = FormHelper.MainForm;
                    formOrg.Show();
                    formOrg.Select();
                }
            }
        }

        #endregion

    }
}
